﻿using System;
using System.Collections.Generic;

/// <summary>
/// 消息中心（单例模式）
/// 功能：统一管理游戏内事件订阅与发布
/// </summary>
public class MessageCenter : Singleton<MessageCenter>
{
    // 消息路由表：int=消息ID，Delegate=回调方法集合
    Dictionary<int, Delegate> dic = new Dictionary<int, Delegate>();

    // 无参数消息系统 --------------------------------------------------
    /// <summary>
    /// 订阅事件
    /// </summary>
    /// <param name="name">消息ID</param>
    /// <param name="action">回调方法</param>
    public void ListenMess(int name, Action action)
    {
        if (dic.ContainsKey(name))
        {
            dic[name] = (Action)dic[name] + action; // 多播委托叠加
        }
        else
        {
            dic.Add(name, action);
        }
    }

    /// <summary>
    /// 取消订阅
    /// </summary>
    public void RemoveMess(int name, Action action)
    {
        if (dic.ContainsKey(name))
        {
            dic[name] = (Action)dic[name] - action; // 多播委托移除
        }
    }

    /// <summary>
    /// 触发事件
    /// </summary>
    public void BuredMess(int name)
    {
        if (dic.ContainsKey(name))
        {
            (dic[name] as Action)?.Invoke(); // 空值安全调用
        }
    }

    // 泛型参数消息系统（支持单个参数）---------------------------------
    /// <summary>
    /// 订阅带参数的事件
    /// </summary>
    /// <typeparam name="T">参数类型</typeparam>
    public void ListenMess<T>(int name, Action<T> action)
    {
        if (dic.ContainsKey(name))
        {
            dic[name] = (Action<T>)dic[name] + action;
        }
        else
        {
            dic.Add(name, action);
        }
    }

    /// <summary>
    /// 取消带参数的订阅
    /// </summary>
    public void RemoveMess<T>(int name, Action<T> action)
    {
        if (dic.ContainsKey(name))
        {
            dic[name] = (Action<T>)dic[name] - action;
        }
    }

    /// <summary>
    /// 触发带参数的事件
    /// </summary>
    /// <param name="t">事件参数</param>
    public void BuredMess<T>(int name, T t)
    {
        if (dic.ContainsKey(name))
        {
            (dic[name] as Action<T>)?.Invoke(t);
        }
    }
}

/// <summary>
/// 消息ID定义类（1000-1999为系统保留范围）
/// </summary>
public class MessId
{
    /// <summary>
    /// 刷新主面板金币数据
    /// </summary>
    public const int RefureCurrencyData = 1001;
    /// <summary>
    /// 刷新主面板金币显示
    /// </summary>
    public const int RefureCurrencyDataText = 1002;
    /// <summary>
    /// 刷新当前等级
    /// </summary>
    public const int RefureNowPlayerLevelData = 1003;
    /// <summary>
    /// 刷新当前皮肤显示
    /// </summary>
    public const int SkinIconRefureShow = 1004;
    /// <summary>
    /// 打开提示面板
    /// </summary>
    public const int OpenTipsPanel = 1005;
    /// <summary>
    /// 观看完广告隐藏对应的Icon
    /// </summary>
    public const int ConcealAdvertising = 1006;
    /// <summary>
    /// 游戏排名显示
    /// </summary>
    public const int RefureRankingList = 1007; 
    /// <summary>
    /// 游戏结束
    /// </summary>
    public const int GameOver = 1008; 
    public const int OpenLextObj = 1009; 
    public const int CloseLextObj = 1010; 
}
